﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Collections;
using WebSilverlight.Web;
using WebSilverlight.Helpers;

namespace WebSilverlight.Views
{
    public partial class CiudadWorkWith : Page
    {
	#region Properties	
     
	#endregion
	
        public CiudadWorkWith()
        {            
            InitializeComponent();
            if(this.dcCiudadContext1 == null)                
				this.dcCiudadContext1 = new CiudadDomainContext();            
        }    
        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {            
        }                
        private void AddNew_Click(object sender, RoutedEventArgs e)
        {
			CallEdit(false, true);
        }
        private void OpenButton_Click(object sender, RoutedEventArgs e)
        {
             CallEdit(true, false);
        }
        private void EditButton_Click(object sender, RoutedEventArgs e)
        {
             CallEdit(false, false);
        }
        private void CallEdit(bool IsReadOnly, bool IsNew)
        {
			CiudadWrapper current = null;
            bool error = false;
            IList list = dataGridCiudad1.SelectedItems;
            
            if (IsNew)
                 current = new CiudadWrapper();
            else
            {
                 if (list.Count != 1)
                 {
                     System.Windows.MessageBox.Show("select one element");
                     error = true;
                 }
                 else
                     current = (CiudadWrapper)list[0];
            }
            if (!error)
			{   
				 if (ddsCiudad1.HasChanges)
				 {                     
					 System.Windows.MessageBox.Show("Changes Discarded");                     
					 this.dataGridCiudad1.CancelEdit();
					 this.ddsCiudad1.RejectChanges();
				 }                

				 var editw = new CiudadEditWindow(!IsReadOnly);				 
				 editw.DomainContext = this.dcCiudadContext1;
				 editw.DDS = this.ddsCiudad1;
				 editw.Current = current;
				 editw.IsNew = IsNew;
				 if(IsNew)
                     editw.Closed += new EventHandler(editw_Closed);
				 editw.Show();
			 }       
        }

        void editw_Closed(object sender, EventArgs e)
        {   
			//Duplicate Entity Key W.A.
			this.dcCiudadContext1 = new CiudadDomainContext();            
            this.ddsCiudad1.Load();            
        }

        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
			if(this.dataGridCiudad1.SelectedItems.Count == 1)
            {
                CiudadWrapper current = (CiudadWrapper)this.dataGridCiudad1.SelectedItem;
                this.dcCiudadContext1.CiudadWrappers.Remove(current);
            }

        }
        private void SubmitButton_Click(object sender, RoutedEventArgs e)
        {        				 
			this.dataGridCiudad1.CommitEdit(DataGridEditingUnit.Row, true);
            this.ddsCiudad1.SubmitChanges();
            this.GridActionsBottom.Visibility = System.Windows.Visibility.Collapsed;
        }
        private void CancelEditButton_Click(object sender, RoutedEventArgs e)
        {
			if (ddsCiudad1.HasChanges)
			{			
				 dataGridCiudad1.CancelEdit();
				 ddsCiudad1.RejectChanges();
			}
			this.GridActionsBottom.Visibility = System.Windows.Visibility.Collapsed;
        }
        private void dataGridCiudad1_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
        {
            this.GridActionsBottom.Visibility = System.Windows.Visibility.Visible;
        } 
	
		/// <summary>
		/// Prompt Call event. 		
		/// </summary> 
        private void PaisIdColumn_Prompt(object sender, RoutedEventArgs e)
        {
			var prompt = new PaisPrompt();
            prompt.Closed += new EventHandler(PaisIdColumn_Prompt_Closed);
            prompt.Show();
        }  
        /// <summary>
		/// Prompt CallBack. Sets the prompt return
		/// </summary>       
		void PaisIdColumn_Prompt_Closed(object sender, EventArgs e)
        {
            var prompt = (PaisPrompt)sender;
            prompt.Closed -= this.PaisIdColumn_Prompt_Closed;

            if (prompt.Selected != null)
            {				 
				 this.GridActionsBottom.Visibility = System.Windows.Visibility.Visible;	 
                 CiudadWrapper Current = (CiudadWrapper)this.dataGridCiudad1.SelectedItem;
                 Current.PaisId = prompt.Selected.PaisId;
                 Current.PaisNombre = prompt.Selected.PaisNombre;
                 Current.PaisMoneda = prompt.Selected.PaisMoneda;
			}
        }         
		 		
    }
}
